package com.winz.cultureapp.viewpage;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.vp.WelcomeVPAdapter;
import com.winz.cultureapp.widge.NavView;

import java.util.ArrayList;
import java.util.List;

/**
 * 欢迎页Viewpager
 */
public class WelcomeViewPager extends FrameLayout implements ViewPager.OnPageChangeListener {
    private static final String SHAREDPREFERENCES_NAME = "first_pref";
    private final Context context;
    private ViewPager viewPager;
    private NavView navView;
    private FragmentManager fm;
    private List<Integer> mDatas;

    private StartListener listener;
    private Button mBtn_start;

    public WelcomeViewPager(Context context, FragmentManager fm) {
        super(context);
        this.context = context;
        this.fm = fm;
        init();
    }

    /**
     * 初始化和下载数据
     */
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.vp_welcome, this, true);
        viewPager = (ViewPager) findViewById(R.id.vp_welcome);
        navView = (NavView) findViewById(R.id.navview);
        mBtn_start = (Button) findViewById(R.id.btn_start);
        viewPager.addOnPageChangeListener(this);
        
        mDatas = new ArrayList<>();
        loadDatas();

        mBtn_start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences preferences = context.getSharedPreferences(
                        SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("isFirstIn", false);
                editor.commit();

                listener.start();
            }
        });
    }

    /**
     * 下载数据
     */
    private void loadDatas() {
        mDatas.add(R.drawable.splash_1);
        mDatas.add(R.drawable.splash_1);
        mDatas.add(R.drawable.splash_1);
//        mDatas.add(R.drawable.splash_2);
//        mDatas.add(R.drawable.splash_3);
        WelcomeVPAdapter mAdapter = new WelcomeVPAdapter(fm,mDatas);
        viewPager.setAdapter(mAdapter);
        navView.setCount(mDatas.size());
        
    }

    /**
     *ViewPager的滑动监听
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(navView.getCount()>0){
            if (position == navView.getCount() - 1) {
                navView.setNavAddress(position, 0);
            } else {
                navView.setNavAddress(position, positionOffset);
            }
        }
    }
    @Override
    public void onPageSelected(int position) {
        if (position == mDatas.size() - 1){
            mBtn_start.setVisibility(VISIBLE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void setStartListener(StartListener listener){
        this.listener = listener;
    }

    public interface StartListener{
        void start();
    }
    
    

}
